package com.attendancesystem.service;

import com.attendancesystem.model.Admin;
import com.attendancesystem.model.Student;
import com.attendancesystem.model.Teacher;
import com.attendancesystem.model.User;
import com.attendancesystem.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserService {
    public List<User> getInfoUsers() {
        List<User> users = new ArrayList<>();
        String sql = "select * from user";
        try (Connection conn = JDBCUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    User user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                    user.setRole(User.UserRole.valueOf(rs.getString("role")));
                    users.add(user);
                }
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }

    public Boolean addUser(User user) {
        if (isUsernameExists(user.getUsername())) {
            return false;
        }
        String sql = "insert into user(username,password,role) values(?,?,?)";
        try (Connection conn = JDBCUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, user.getUsername());
            pstmt.setString(2, user.getPassword());
            pstmt.setString(3, user.getRole().toString());
            int i = pstmt.executeUpdate();
            if (i > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean isUsernameExists(String username) {
        String sql = "SELECT COUNT(*) FROM user WHERE username = ?";
        try (Connection conn = JDBCUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, username);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return rs.getInt(1) > 0;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean deleteUser(int id) {
        String sql = "DELETE FROM user WHERE id = ?";
        try (Connection conn = JDBCUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, id);
            int i = pstmt.executeUpdate();
            if (i > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public Object editUser(String username, String role) {
        String sql = "";
        try (Connection conn = JDBCUtils.getConnection()) {
            if (role.equals("ADMIN")) {
                sql = "SELECT * FROM administrators WHERE username = ?";
                try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                    pstmt.setString(1, username);
                    try (ResultSet rs = pstmt.executeQuery()) {
                        if (rs.next()) {
                            Admin admin = new Admin();
                            admin.setUsername(rs.getString("username"));
                            admin.setDepartment(rs.getString("department"));
                            admin.setFullName(rs.getString("full_name"));
                            admin.setContactPhone(rs.getString("contact_phone"));
                            admin.setEmail(rs.getString("email"));
                            admin.setOffice(rs.getString("office"));
                            return admin;
                        }
                    }
                }

            } else if (role.equals("TEACHER")) {
                sql = "SELECT * FROM teachers WHERE employee_id = ?";
                try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                    pstmt.setString(1, username);
                    try (ResultSet rs = pstmt.executeQuery()) {
                        if (rs.next()) {
                            Teacher teacher = new Teacher();
                            teacher.setEmployeeId(rs.getString("employee_id"));
                            teacher.setName(rs.getString("name"));
                            teacher.setDepartment(rs.getString("department"));
                            teacher.setTitle(rs.getString("title"));
                            teacher.setPhone(rs.getString("phone"));
                            teacher.setEmail(rs.getString("email"));
                            teacher.setOffice(rs.getString("office"));
                            // 设置其他教师特有字段
                            return teacher;
                        }
                    }
                }
            } else if (role.equals("STUDENT")) {
                sql = "SELECT * FROM students WHERE id = ?";
                try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                    pstmt.setString(1, username);
                    try (ResultSet rs = pstmt.executeQuery()) {
                        if (rs.next()) {
                            Student student = new Student();
                            student.setId(rs.getString("id"));
                            student.setName(rs.getString("name"));
                            student.setCollege(rs.getString("college"));
                            student.setClassName(rs.getString("class"));
                            // 设置其他学生特有字段
                            return student;
                        }
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}

